|
|
|
|
|
|
|
The left side of this assignment statement, txtArray(Index), refers to a specific element of the txtArray control array. Which element? The array subscript Index, which is passed into the event procedure as a parameter, allows you to determine which element of the control array just lost the focus (causing this event to fire). Therefore, this line of code assigns to that specific control array element the value of the expression on the right side of the assignment statementin this case, the contents of that array element's default property (Text), converted to proper case via the StrConv function. |
|
|
|
|
|
|
|
|
After you close the Code window, double-click one of the other elements of the text box control array. Notice that the Code window opens to the exact same event procedure. Remember, control array elements share event procedures. |
|
|
|
|
|
|
|
|
Now try your sample application. Start by running the program; then type TINA MARIE in uppercase letters in the first text box (next to the First Name label). When you press Tab to move to the next text box, the LostFocus event fires and the text that you typed is converted to proper case. Type jones (all lowercase) in the Last Name text box. Press Tab again, and notice that this text is also converted to proper case. The LostFocus event procedure that you wrote is executed whenever the LostFocus event occurs to any of the controls in the array. |
|
|
|
|
|
|
|
|
Isolating Control Array Elements with the Index Property |
|
|
|
|
|
|
|
|
If you need to execute code only for one or more specific members of the array, your code can use the Index argument to determine which array element fired the event. Then you can, for example, place the necessary code within an If block or Select Case block. |
|
|
|
|
|
|
|
|
Examine this use with the sample project. The code that converts the users' entries into proper case is working fine, but state abbreviations should be all uppercase. Therefore, you should modify the txtArray_LostFocus event procedure to isolate the state text box (array element number 3) and perform a different action for it. |
|
|
|
|
|
|
|
|
Modify the code in the txtArray_LostFocus event procedure to match the following: |
|
|
|
|
|
|
|
|
If Index = 3 Then
txtArray(Index) = UCase(txtArray(Index))
Else
txtArray(Index) = StrConv(txtArray(Index), vbProperCase)
End If |
|
|
|
|
|
|
|
|
Try this new event procedure by running the program and using all lowercase letters to type your first and last name, city, and state in the appropriate text boxes. Notice how the event procedure modifies the text in the State text box differently than the others. |
|
|
|
|
|
|
|
|
Manipulating Multiple Controls in Code |
|
|
|
|
|
|
|
|
Another benefit of using a control array is that you can set properties for each element in an array at runtime by using a For . . . Next loop. This way, your program can easily work with all the controls in a control array. For example, the following lines of code convert the text in all the members of a text box control array into uppercase: |
|
|
|
|
|
|
|
|
Dim i As Integer
For i = 0 To 3
txtArray(i) = UCase(txtArray(i))
Next i |
|
|
|
|
|